package rinpu.springtest.test.test;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import rinpu.springtest.pojo.entity.User;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

@Service
@Slf4j
public class AsyncService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Async("asyncServiceExecutor")
    public CompletableFuture<Integer> asyncInsertBatch(List<User> list, CountDownLatch countDownLatch) {
        int row = 0;
        log.info("线程-{}在执行写入", Thread.currentThread().getId());
        try {
            row += testMapper.insertBatch(list);
            if (Math.random() > 0.7) {
                throw new RuntimeException("运气有点不好哦");
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            countDownLatch.countDown();
        }
        return CompletableFuture.completedFuture(row);
    }

    @Async("asyncServiceExecutor")
    public void asyncInsertBatchTwo(List<User> list, CountDownLatch countDownLatch, AtomicBoolean isSuccess) {
        log.info("线程-{}在执行写入", Thread.currentThread().getId());
        TransactionStatus transactionStatus = platformTransactionManager
                .getTransaction(new DefaultTransactionDefinition());
        try {
            testMapper.insertBatch(list);
            if (Math.random() > 0.9) {
                throw new RuntimeException("运气有点不好哦");
            }
        } catch (Exception e) {
            isSuccess.set(Boolean.FALSE);
            throw new RuntimeException(e.getMessage());
        } finally {
            countDownLatch.countDown();
        }
        try {
            countDownLatch.await();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        if (isSuccess.get()) {
            platformTransactionManager.commit(transactionStatus);
        } else {
            platformTransactionManager.rollback(transactionStatus);
        }
    }

    @Async("AsyncServiceExecutor")
    public void asyncInsertBatchThree(List<User> list, CountDownLatch countDownLatch, AtomicBoolean isSuccess) {
        log.info("线程-{}在执行写入", Thread.currentThread().getId());
        try {
            testMapper.insertBatch(list);
            if (Math.random() > 0.9) {
                throw new RuntimeException("运气有点不好哦");
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            countDownLatch.countDown();
        }
    }
}
